package com.example.work12;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.work12.adapter.WelcomeAdapter;
import com.example.work12.fragment.OneFragment;
import com.example.work12.fragment.TwoFragment;
import com.example.work12.fragment.WelcomeFragment;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends AppCompatActivity {
    private ViewPager mVp;
    private List<Fragment> fragmentList;
    private LinearLayout mLl;
    private SharedPreferences sp;
    private List<ImageView> imageViews = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);


        sp = getSharedPreferences("music", MODE_PRIVATE);
        boolean flag = sp.getBoolean("flag", false);
        if(flag){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }

        mVp = (ViewPager) findViewById(R.id.vp);
        mLl = (LinearLayout) findViewById(R.id.ll);


        fragmentList = new ArrayList<>();
        fragmentList.add(new OneFragment());
        fragmentList.add(new TwoFragment());
        fragmentList.add(new WelcomeFragment());

        WelcomeAdapter welcomeAdapter = new WelcomeAdapter(getSupportFragmentManager(), fragmentList);

        mVp.setAdapter(welcomeAdapter);

        for (int i = 0; i <fragmentList.size() ; i++) {
            ImageView imageView = new ImageView(this);
            if(i == 0){
                imageView.setImageResource(R.drawable.ciacre);
            }else {
                imageView.setImageResource(R.drawable.ciacer_t);
            }
            imageViews.add(imageView);
            mLl.addView(imageView);
        }

        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if(i == fragmentList.size() -1 ){
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putBoolean("flag",true);
                    edit.commit();
                }
                for (int j = 0; j < imageViews.size() ; j++) {
                    if(i == j){
                        imageViews.get(j).setImageResource(R.drawable.ciacre);
                    }else {
                        imageViews.get(j).setImageResource(R.drawable.ciacer_t);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        for (int i = 0; i < imageViews.size() ; i++) {
            final int finalI = i;
            imageViews.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mVp.setCurrentItem(finalI);
                }
            });
        }
    }
}
